﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public class ReceiveParser
    {
        protected IRCForm form;
        protected Channel channel;
        public ReceiveParser(IRCForm form)
        {
            this.form = form;
        }
        
        public String parser(String incoming)
        {
            //ENDURSKRIFA ÞETTA ER ÓGEÐ
            //Parse user list and channel name on join
            if (incoming.Contains("353 "))
            {
                List<string> names = new List<string>();
                string channelName;
                string[] parameters;

                int channelBegin = incoming.IndexOf("#");
                channelName = incoming.Substring(channelBegin);
                int channelEnd = channelName.IndexOf(":");
                string z = channelName.Substring(channelEnd);
                string l = channelName.Substring(0, (channelName.Length - z.Length)).Trim();
                
                int index = incoming.IndexOf("@");
                string x = incoming.Substring(index);
                int index2 = x.IndexOf(":");
                string y = x.Substring(index2 + 1);
                char[] splitchar = { ' ' };
                parameters = y.Split(splitchar);

                foreach (var name in parameters)
                {
                    names.Add(name);  
                }
                channel = new Channel(l, names);
                form.AddToListBox(channel);
            }

            if(incoming.Contains("PART"))
            {
                form.cleanListBox();
            }
            return incoming;
        }

    }
}
